package com.michael.common.result;

import com.michael.common.error.code.BizCode;
import lombok.Data;

import java.io.Serializable;

@Data
public class BaseResult<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private T data;
    private int ret;
    private String msg;

    public BaseResult(int ret, String msg, T data) {
        this.data = data;
        this.ret = ret;
        this.msg = msg;
    }

    public BaseResult() {
    }

    public boolean success() {
        return this.getRet() == BizCode.SUCCESS.getRet();
    }

    public boolean fail() {
        return !success();
    }

    public static <T> BaseResult<T> errorOf(BaseResult result) {
        return new BaseResult<>(result.getRet(), result.getMsg(), null);
    }

    public static <T> BaseResult<T> errorOf(BizCode errorCode) {
        return new BaseResult<>(errorCode.getRet(), errorCode.getMsg(), null);
    }

    public static <T> BaseResult<T> errorOf(BizCode errorCode, String msg) {
        return new BaseResult<>(errorCode.getRet(), msg, null);
    }

    public static <T> BaseResult<T> errorOf(Integer errorRet, String errorMsg) {
        return new BaseResult<T>(errorRet, errorMsg, null);
    }

    public static <T> BaseResult<T> getError(Integer errorRet, String errorMsg) {
        return new BaseResult<T>(errorRet, errorMsg, null);
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public int getRet() {
        return ret;
    }

    public static <T> BaseResult<T> successOf(T data) {
        return new BaseResult<T>(BizCode.SUCCESS.getRet(), BizCode.SUCCESS.getMsg(), data);
    }
}
